import time

from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

# 1.创建驱动对象
service = Service("C:/Program Files/Google/Chrome/Application/chromedriver.exe")
driver = webdriver.Chrome(service=service)

# 2.打开浏览器
driver.get("file:///C:/Users/82073/Desktop/pagetest/%E6%B3%A8%E5%86%8CA.html")

# 0.隐式元素等待
driver.implicitly_wait(10)

# 1.获取当前窗口句柄, 并打印
print("切换前, 窗口句柄为: ", driver.current_window_handle)

# 2.在注册A页面点击 "访问 新浪 网站"
driver.find_element(By.ID, "fw").click()

# 3.获取所有窗口句柄
handlers = driver.window_handles
print("所有窗口句柄为: ", handlers)

# 4.根据句柄, 切换到新浪窗口
# 注: 新浪页面加载慢, 可能需要用到元素等待
driver.switch_to.window(handlers[1])
print("切换后, 窗口句柄为: ", driver.current_window_handle)

# 5.对新浪输入框输入内容
driver.find_element(By.CLASS_NAME, "inp-txt").clear()
time.sleep(1)
driver.find_element(By.CLASS_NAME, "inp-txt").send_keys("搜索新浪")

time.sleep(2)
# 6.切换回原窗口
driver.switch_to.window(handlers[0])
print("切换回原窗口, 句柄为: ", driver.current_window_handle)

# 7.输入用户名 admin
driver.find_element(By.ID, "userA").send_keys("admin")

# 4.3s后关闭
time.sleep(3)

# 5.关闭驱动对象
driver.close()